/**  
 * All rights Reserved, Designed By daiw
 * @Title:  SysDeptController.java   
 * @Package org.easy.system.manage.controller   
 * @Description:    TODO (用一句话描述该文件做什么)   
 * @author: daiw     
 * @date:   2019年10月21日 上午10:36:44   
 * @Copyright: 2019 daiw Inc. All rights reserved. 
 */
package org.easy.system.manage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.easy.system.manage.dto.DeptDto;
import org.easy.system.manage.dto.DeptQueryParam;
import org.easy.system.manage.dto.SaveDeptParam;
import org.easy.system.manage.service.SysDeptService;
import org.easyframework.core.entity.ProcessResult;
import org.easyframework.core.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.system.manage.facade.entity.UserInfoDto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: SysDeptController
 * @Description: 部门管理
 * @author: daiw
 * @date: 2019年10月21日 上午10:36:44
 */
@Api("部门管理")
@Controller
@RequestMapping("dept")
public class SysDeptController extends BaseController {

	@Autowired
	private SysDeptService sysDeptService;

	@ApiOperation(value = "查询顶级部门")
	@RequestMapping(value = "queryTopDept", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<DeptDto> queryTopDept() {
		ProcessResult<DeptDto> processResult = sysDeptService.queryTopDept();
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "根据父级id查询子部门")
	@ApiImplicitParam(value = "父级部门编号", name = "parentId", paramType = "form", required = true, dataType = "String")
	@RequestMapping(value = "queryByParentId", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<List<DeptDto>> queryByParentId(@RequestParam(required = true) String parentId) {
		ProcessResult<List<DeptDto>> processResult = sysDeptService.queryByParentId(Long.parseLong(parentId));
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "保存部门信息")
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<?> save(@RequestBody SaveDeptParam saveDeptParam, HttpServletRequest request) {
		UserInfoDto userInfo = getUserInfo(request);
		ProcessResult<?> processResult = sysDeptService.save(saveDeptParam, userInfo);
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "更新部门信息")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<?> update(@RequestBody DeptDto deptDto, HttpServletRequest request) {
		UserInfoDto userInfo = getUserInfo(request);
		ProcessResult<?> processResult = sysDeptService.update(deptDto, userInfo);
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "分页查询部门信息")
	@RequestMapping(value = "queryByPage", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<List<DeptDto>> queryByPage(@RequestBody DeptQueryParam deptQueryParam, HttpServletRequest request) {
		ProcessResult<List<DeptDto>> processResult = sysDeptService.queryByPage(deptQueryParam);
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "查询部门信息详情")
	@ApiImplicitParam(value = "部门编号", name = "deptId", paramType = "form", required = true, dataType = "String")
	@RequestMapping(value = "queryDetail", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<DeptDto> queryDetail(@RequestParam(required = true) String deptId) {
		ProcessResult<DeptDto> processResult = sysDeptService.queryDetail(Long.parseLong(deptId));
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "更新部门状态")
	@ApiImplicitParams(value = { //
			@ApiImplicitParam(value = "部门编号", paramType = "form", name = "deptId", required = true, dataType = "String"), //
			@ApiImplicitParam(value = "部门状态（0正常 1停用）", paramType = "form", name = "status", required = true, dataType = "Integer") })
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<?> updateStatus(@RequestParam(required = true) String deptId, @RequestParam(required = true) Integer status, HttpServletRequest request) {
		UserInfoDto userInfo = getUserInfo(request);
		ProcessResult<?> processResult = sysDeptService.updateStatus(Long.parseLong(deptId), status, userInfo);
		return ResponseResult.getInst(processResult);
	}
}
